/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package flightlifter;

import java.util.Vector;

/**
 *
 * @author Matt
 */
public class Coupling extends LineConstraint implements WiringConsumer {

    boolean released = false;
    float hangLength;

    public Coupling(Point a, Point b) {
        super(a, b);
        red = 1.f;
        hangLength = length;

    }

    public void render(int mode){
        if (!released) super.render(mode);
    }

    public void physics() {
        if (!released) {
            
            if (length > .01f){
                length -= length/100;
            }else{
                length = .01f;
            }

            float dx = b.pos.x - a.pos.x;
            float dy = b.pos.y - a.pos.y;
            float dz = b.pos.z - a.pos.z;

            float d1 = (float) Math.sqrt(dx * dx + dy * dy + dz * dz);


            //find the mass divisor
            float arat = b.mass / (a.mass + b.mass);
            float brat = a.mass / (a.mass + b.mass);



            float d2 = (d1 - length) / d1;
            dx *= d2;
            dy *= d2;
            dz *= d2;
             
           

            a.pos.x += dx * arat;
            a.pos.y += dy * arat;
            a.pos.z += dz * arat;

            b.pos.x -= dx * brat;
            b.pos.y -= dy * brat;
            b.pos.z -= dz * brat;
          
        }

    }

    @Override
    public void setValue(float value) {
        if (value > .9f) {
            released = true;
        }
    }

    @Override

    public PVector getScreenCenter() {
        return getScreenSpaceCentroid();
    }

        public Coupling(String [] line, Vector <Node> nodes ){
        this((Point)nodes.elementAt(Integer.parseInt(line[1])),
             (Point)nodes.elementAt(Integer.parseInt(line[2])));

             hangLength = Float.parseFloat(line[3]);
    }

    public PVector getScreenSpaceCentroid() {
        float[] ret = GLProject.project((b.pos.x - a.pos.x) / 2 + a.pos.x,
                (b.pos.y - a.pos.y) / 2 + a.pos.y,
                (b.pos.z - a.pos.z) / 2 + a.pos.z);

        return new PVector(ret[0], ret[1], 0);
    }



    public String pickle(Vector <Node> nodes) {
        return "U," +
                nodes.indexOf(a) + "," +
                nodes.indexOf(b) + "," +
                hangLength;


    }


}
